using Iesi.Collections.Generic;
using NHibernate.Mapping.Attributes;

namespace Bussiness.Model
{
    [Class(Name = "Bussiness.Model.Category, Bussiness")]
    public class Category : BaseModel
    {
        private string _name;
        private string _resourceKey;
        private string _description;
        private Category _parent;
        private readonly ISet<Category> _children = new HashedSet<Category>();
        private readonly ISet<New> _news = new HashedSet<New>();

        #region Constucters
        /// <summary>
        /// No-arg constructor for tools.
        /// </summary>
        public Category() { }

        /// <summary>
        /// Simple constructor
        /// </summary>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="resourceKey"></param>
        public Category(string name, string description, string resourceKey)
        {
            _name = name;
            _description = description;
            _resourceKey = resourceKey;
        }


        /// <summary>
        /// Full constuco
        /// </summary>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="resourceKey"></param>
        /// <param name="parent"></param>
        public Category(string name, string description, string resourceKey, Category parent)
        {
            _name = name;
            _description = description;
            _resourceKey = resourceKey;
            _parent = parent;
        }

        #endregion

        #region Properties
        [Property(NotNull = true, Length = 100)]
        public virtual string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        [Property(NotNull = true, Length = 100)]
        public virtual string ResourceKey
        {
            get { return _resourceKey; }
            set { _resourceKey = value; }
        }

        [Property(Length = 200)]
        public virtual string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        [ManyToOne(0, OuterJoin = OuterJoinStrategy.True, Cascade = "save-update")]
        [Column(1, Name = "PARENT_ID", NotNull = false)]
        public virtual Category Parent
        {
            get { return _parent; }
            set { _parent = value; }
        }


        [Set(0, Cascade = "all-delete-orphan", Inverse = true, Access = "nosetter.camelcase-underscore")]
        [Key(1, Column = "PARENT_ID")]
        [OneToMany(2, ClassType = typeof(Category))]
        public virtual ISet<Category> Children
        {
            get { return _children; }
        }

        [Set(0, Cascade = "all-delete-orphan", Inverse = true, Access = "nosetter.camelcase-underscore")]
        [Key(1, Column = "CATEGORY_ID")]
        [OneToMany(2, ClassType = typeof(New))]
        public virtual ISet<New> News
        {
            get { return _news; }
        } 
        #endregion

        #region Common methods
        public override bool Equals(object obj)
        {
            if (this == obj) return true;
            if (!(obj is Category)) return false;
            var that = (Category)obj;
            if (this.Parent != null ? this.Parent != that.Parent : that.Parent != null) return false;
            if (this.Name != that.Name) return false;
            if (this.Description != null ? this.Description != that.Description : that.Description != null) return false;
            return true;
        }

        public override int GetHashCode()
        {
            int result = this.Name.GetHashCode();
            if (this.Description != null) result = 29 * result + this.Description.GetHashCode();
            result = 29 * result + this.ResourceKey.GetHashCode();
            return result;
        } 
        #endregion

    }
}
